package jjn.carl.binary_tree;

import commons.BinaryTreeHelper;
import commons.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-07-12 0:06
 */
public class LeetCode145 {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        dfs(root, result);
        return result;
    }
    
    private void dfs(TreeNode node, List<Integer> result) {
        if (node == null) {
            return;
        }
        dfs(node.left, result);
        dfs(node.right, result);
        result.add(node.val);
    }
    
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>();
        input.add(1);
        input.add(null);
        input.addAll(List.of(2, 3));
        TreeNode root = BinaryTreeHelper.buildTree(input);
        List<Integer> list = new LeetCode145().postorderTraversal(root);
        System.out.println("list = " + list);
    }
}
